package com.raibaz.atm;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.raibaz.atm.businesslogic.AtmFetcher;
import com.raibaz.atm.businesslogic.Instruction;

public class Atm extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                
        Button goBtn = (Button)findViewById(R.id.ButtonGo);                               
        goBtn.setOnClickListener(new OnClickListener() {			
			
			public void onClick(View v) {
				EditText etFrom = (EditText)findViewById(R.id.From);
		        EditText etTo = (EditText)findViewById(R.id.To);
		        final String from = etFrom.getText().toString();
		        final String to = etTo.getText().toString();
				ArrayList<Instruction> instructions = AtmFetcher.fetchPath(from, to);
				Intent intent = new Intent(v.getContext(), InstructionsActivity.class);
				intent.putExtra("instructions", instructions);
				startActivity(intent);								
			}
		});                     
        
        Button locateBtn = (Button)findViewById(R.id.ButtonLocateMe);
        locateBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Button caller = (Button)v;
				caller.setText("mmmm....");
				LocationManager lm = (LocationManager)Atm.this.getSystemService(Context.LOCATION_SERVICE);
				
				Criteria locationCritera = new Criteria();
		        locationCritera.setAccuracy(Criteria.ACCURACY_COARSE);
		        locationCritera.setAltitudeRequired(false);
		        locationCritera.setBearingRequired(false);
		        locationCritera.setCostAllowed(true);
		        locationCritera.setPowerRequirement(Criteria.NO_REQUIREMENT);
		        
		        String providerName = lm.getBestProvider(locationCritera, true);

		        if (providerName != null && lm.isProviderEnabled(providerName)) {
		            // Provider is enabled
		            lm.requestLocationUpdates(providerName, 20000, 100, new AtmLocationListener(Atm.this));
		        } else {
		            // Provider not enabled, prompt user to enable it
		            Toast.makeText(Atm.this, "Turn on gps", Toast.LENGTH_LONG).show();
		            Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		            Atm.this.startActivity(myIntent);
		        }											
			}
		});
    }
    
    public void gpsLocationReceived(String address) {    	
    	EditText from = (EditText)findViewById(R.id.From);
    	from.setText(address);
    	Button locateBtn = (Button)findViewById(R.id.ButtonLocateMe);
    	locateBtn.setText(R.string.locate);
    }
}